﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models.Car_Li;
using ICarlifeRespository;
using Commonality.DTO.Car_Li;
using Carlife.Inf;
using System.Collections;
using System.Xml.Linq;
using ICarlifeService;

namespace CarlifeService
{
    public class SalelistService : BaseServer<Salesperformance>, ISalesService
    {
        private readonly ISalesperformanceRespository db;
        private readonly ICarMarketRespository carMarket;
        private readonly IClientdisposeRespository clientdispose;
        private readonly IDictionariesRespository dictionaries;
        private readonly IUOfWork uOfWork;
        public SalelistService(ISalesperformanceRespository db, IUOfWork uOfWork, ICarMarketRespository carMarket, IClientdisposeRespository clientdispose, IDictionariesRespository dictionaries) : base(db, uOfWork)
        {
            this.db = db;
            this.uOfWork = uOfWork;
            this.carMarket = carMarket;
            this.clientdispose = clientdispose;
            this.dictionaries = dictionaries;
        }

        public CarSalesFenye CarList(string? name = "", string ctime = "", string Utime = "", int pagesize = 3, int pageindex = 1)
            {
                var list = from a in dictionaries.GetAll()
                           join b in carMarket.GetAll() on a.Tid equals b.Tid
                           join c in db.GetAll() on b.SalesId equals c.Id
                           join d in clientdispose.GetAll() on c.Clientd equals d.Id
                           select new CarSalesDTO
                           {
                               MarketId = b.Id,
                               MarketName = b.MarketName,
                               MarketSeries = b.MarketSeries,
                               Tname = a.Tname,
                               SalesNumber = c.SalesNumber,
                               Marketcard = b.Marketcard,
                               MarketEngine = b.MarketEngine,
                               MarketCreateDate = b.MarketCreateDate,
                               MarketUpdateDate = b.MarketUpdateDate,
                               MarketNum = b.MarketNum,
                               MarketColor = b.MarketColor,
                               SalesId = c.Id,
                               SalesName = c.SalesName,
                               Cname = a.Cname,
                               Salestype = c.Salestype,
                               SalesMoney = c.SalesMoney,
                               Servicename = d.Servicename,
                               SaleEnterDate = c.SaleEnterDate,
                               Clientd = d.Id,
                               Sclientele = d.Sclientele,
                               Aclientele = d.Aclientele,
                               Bclientele = d.Bclientele,
                               Cclientele = d.Cclientele,
                               Dclientele = d.Dclientele,
                               CREATED_ID = d.Id,
                           };
                if (!string.IsNullOrEmpty(name))
                {
                    list = list.Where(x => x.MarketName.Contains(name));
                }
                if (!string.IsNullOrEmpty(ctime))
                {
                    list = list.Where(x => x.MarketCreateDate >= (Convert.ToDateTime(ctime)));
                }
                if (!string.IsNullOrEmpty(Utime))
                {
                    list = list.Where(x => x.MarketUpdateDate <= (Convert.ToDateTime(Utime)));
                }
                return new CarSalesFenye
                {
                    Rowcount = list.Count(),
                    CData = list.OrderBy(x => x.MarketId).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList()
                };


            

        }
    }
}
